import { requestClient } from '#/api/request';

/**
 * 会员等级查询参数
 */
export interface MembershipLevelListParams {
  pageNo?: number;
  pageSize?: number;
  levelName?: string;
  levelCode?: string;
  periodType?: number;
  isFree?: number;
  sortField?: string;
  isAsc?: number;
}

/**
 * 会员等级数据
 */
export interface MembershipLevel {
  id: number;
  levelName: string;
  levelCode: string;
  gridWidth: number;
  gridHeight: number;
  price: number;
  periodType: number;
  periodTypeText?: string;
  periodValue: number;
  maxContentCount: number;
  isFree: number;
  description: string;
  createdTime?: string;
  updateTime?: string;
}

/**
 * 会员等级表单数据
 */
export interface MembershipLevelFormData {
  id?: number;
  levelName: string;
  levelCode: string;
  gridWidth: number;
  gridHeight: number;
  price: number;
  periodType: number;
  periodValue: number;
  maxContentCount: number;
  isFree: number;
  description: string;
}

/**
 * 会员等级列表响应数据
 */
export interface MembershipLevelListResult {
  pageNo: number;
  pageSize: number;
  result: MembershipLevel[];
  total: number;
  totalPages: number;
  start: number;
  end: number;
}

/**
 * API响应格式
 */
export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

/**
 * 周期类型数据
 */
export interface PeriodType {
  code: number;
  name: string;
  unit: string;
}

/**
 * 周期类型枚举
 */
export const PERIOD_TYPE_OPTIONS = [
  { value: 1, label: '天卡' },
  { value: 2, label: '周卡' },
  { value: 3, label: '月卡' },
  { value: 4, label: '季卡' },
  { value: 5, label: '年卡' },
];

/**
 * 是否免费枚举
 */
export const IS_FREE_OPTIONS = [
  { value: 1, label: '是' },
  { value: 0, label: '否' },
];

/**
 * 获取周期类型列表
 */
export async function getPeriodTypesApi() {
  return requestClient.get<ApiResponse<PeriodType[]>>('/membership-level/periodTypes');
}

/**
 * 获取会员等级列表（分页）
 */
export async function getMembershipLevelListApi(params?: MembershipLevelListParams) {
  return requestClient.get<ApiResponse<MembershipLevelListResult>>('/membership-level/list', { params });
}

/**
 * 获取会员等级详情
 */
export async function getMembershipLevelDetailApi(id: number) {
  return requestClient.get<ApiResponse<MembershipLevel>>(`/membership-level/${id}`);
}

/**
 * 根据等级编码获取会员等级
 */
export async function getMembershipLevelByCodeApi(levelCode: string) {
  return requestClient.get<ApiResponse<MembershipLevel>>(`/membership-level/code/${levelCode}`);
}

/**
 * 新增会员等级
 */
export async function createMembershipLevelApi(data: MembershipLevelFormData) {
  return requestClient.post<ApiResponse<MembershipLevel>>('/membership-level', data);
}

/**
 * 更新会员等级
 */
export async function updateMembershipLevelApi(data: MembershipLevelFormData) {
  return requestClient.put<ApiResponse<MembershipLevel>>('/membership-level', data);
}

/**
 * 删除会员等级（支持批量）
 */
export async function deleteMembershipLevelsApi(ids: number[]) {
  return requestClient.delete<ApiResponse<string>>('/membership-level', { data: ids });
}

/**
 * 获取周期类型文本
 */
export function getPeriodTypeText(periodType: number): string {
  const option = PERIOD_TYPE_OPTIONS.find(item => item.value === periodType);
  return option ? option.label : '未知';
}

/**
 * 获取是否免费文本
 */
export function getIsFreeText(isFree: number): string {
  return isFree === 1 ? '是' : '否';
} 
 